package ExcelEdit;

import java.io.File;

import com.extentech.ExtenXLS.CellHandle;
import com.extentech.formats.XLS.WorkSheetNotFoundException;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class ExcelEditor extends Application {

	private TableView table = new TableView();

	@Override
	public void start(final Stage stage) throws Exception {

		final FileChooser fileChooser = new FileChooser();
		final Button selectBtn = new Button("Select Excel File");
		selectBtn.setFont(new Font("Arial", 14));
		selectBtn.setAlignment(Pos.CENTER);
		selectBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {

				configureFileChooser(fileChooser);
				File file = fileChooser.showOpenDialog(stage);

				if (file != null) {
					System.out.println(file.getPath());
					ReadExcel excel = new ReadExcel(file.getPath());
					//Set first column name
					TableColumn firstNameCol = new TableColumn("#");
					table.getColumns().add(firstNameCol);
					
					
					try {
						for (CellHandle cell : excel.getSheet(0).getCells()) {
							if (cell.getRowNum() == 2)
								table.getColumns().add(new TableColumn(cell.getStringVal()));
						}
					} catch (WorkSheetNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

			}
		});

		table.setEditable(true);

		final BorderPane border = new BorderPane();
		final HBox hbox = new HBox();
		hbox.setAlignment(Pos.CENTER);
		hbox.getChildren().add(selectBtn);

		border.setTop(hbox);
		border.setCenter(table);

		Scene scene = new Scene(border, 800, 600);

		stage.setScene(scene);
		stage.setTitle("Aufgabenliste");
		stage.show();

	}

	public HBox addHBox() {
		HBox hbox = new HBox();
		return hbox;
	}

	private static void configureFileChooser(final FileChooser fileChooser) {
		fileChooser.setTitle("Open Excel file");
		fileChooser.setInitialDirectory(new File(System
				.getProperty("user.home")));
		fileChooser.getExtensionFilters().addAll(
				new FileChooser.ExtensionFilter("EXCEL 2007-", "*.xlsx"));
	}

	public static void main(String[] args) {
		launch(args);
	}

}
